package com.dshore.messagecenter.util;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 模板匹配
 * @author Administrator
 *
 */
public class MessageMatch {
	
	public static String messageMatch(String template,Map<String,Object> map) {
		StringBuffer sb = new StringBuffer();
		Matcher m = Pattern.compile("\\{\\{\\w*\\}\\}").matcher(template);
		while(m.find()) {
			String param = m.group();
			Object value = map.get(param.substring(2,param.length()-2));
			m.appendReplacement(sb, value==null?param:value.toString());
		}
		m.appendTail(sb);
		return sb.toString();
		
	}
	/*public static void main(String[] args) {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("name", "汤先生");
		map.put("age", 18);
		String message=messageMatch("你好{{name}},你今年{{age}}",map);
		System.out.println(message);
	}*/
}
